package org.jeecg.modules.demo.sy.service.impl;

import org.jeecg.modules.demo.sy.mapper.DeviceAreaLogMapper;
import org.jeecg.modules.demo.sy.mapper.DeviceAreaMapper;
import org.jeecg.modules.demo.sy.mapper.DeviceLogMapper;
import org.jeecg.modules.demo.sy.mapper.DeviceMapper;
import org.jeecg.modules.demo.sy.entity.*;
import org.jeecg.modules.demo.sy.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2022年03月04日 10:26
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    DeviceLogMapper deviceLogMapper;
    @Autowired
    DeviceAreaLogMapper deviceAreaLogMapper;
    @Autowired
    DeviceAreaMapper deviceAreaMapper;

    @Override
    public List<Device> findAllDevices() {
        return deviceMapper.findAllDevices();
    }

    @Override
    public Integer findDevicesAmount() {
        return deviceMapper.findDevicesAmount();
    }

    @Override
    public Integer findDevicesAmountByStatusId(int deviceStatusId) {
        return deviceMapper.findDevicesAmountByStatusId(deviceStatusId);
    }

    @Override
    public List<Mode1> findDevicesTypeAndAmount() {
        return deviceMapper.findDevicesTypeAndAmount();
    }

    @Override
    public List<Mode1> findDevicesStatusAndAmount() {
        return deviceMapper.findDevicesStatusAndAmount();
    }

    @Override
    public void updateDevicesStatus(int deviceId, int nowDeviceStatusId, int nowFaultTypeId) {
        Device device = deviceMapper.findDeviceById(deviceId);
        //得到该设备之前的状态
        DeviceStatus preDeviceStatus = device.getDeviceStatus();
        FaultType preFaultType = device.getFaultType();
        int preDeviceStatusId = preDeviceStatus.getDeviceStatusId();
        int preFaultTypeId = preFaultType.getFaultTypeId();

        Date changeTime = new Date();

        //修改设备的状态
        deviceMapper.updateDevicesStatus(deviceId,nowDeviceStatusId,nowFaultTypeId);

        //插入设备的状态修改日志
        deviceLogMapper.insertDeviceLog(deviceId,preDeviceStatusId,preFaultTypeId,nowDeviceStatusId,nowFaultTypeId,changeTime);

    }

    @Override
    public List<Mode2> findAreaDevicesAmountAndFaultDeviceAmount() {
        return deviceMapper.findAreaDevicesAmountAndFaultDeviceAmount();
    }

    @Override
    public void insertDeviceAreaLog(String areaName, Double faultValue, Double totalValue, Double proportion, Date insertTime) {
        deviceAreaLogMapper.insertDeviceAreaLog(areaName,faultValue,totalValue,proportion,insertTime);
    }

    @Override
    public Integer findAreaAmount() {
        return deviceAreaMapper.findAreaAmount();
    }

    @Override
    public List<Mode4> findExcellentAreaRank(Integer areaAmount) {
        return deviceAreaLogMapper.findExcellentAreaRank(areaAmount);
    }

    @Override
    public int findAreaIdByName(String areaName) {
        return deviceAreaMapper.findAreaIdByName(areaName);
    }


}
